package com.changgou.goods.controller;

import com.changgou.goods.pojo.Spec;
import com.changgou.goods.service.SpecService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author seafwg
 * @Date 2021/11/29 10:21 下午
 * @Version 1.0
 * @description 规格管理：
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 根据分类ID查询规格参数信息
     * @param cid
     * @return
     */
    @GetMapping(value = "/category/{cid}")
    public Result<List<Spec>> findSpecByCategoryId(@PathVariable(name = "cid") Integer cid) {
        List<Spec> specList = specService.findSpecByCategoryId(cid);
        return new Result<List<Spec>>(true, StatusCode.OK, "查询成功", specList);
    }
    /**
     * 新增规格：
     * @param spec
     * @return
     */
    @PostMapping(value = "/add")
    public Result add(@RequestBody Spec spec) {
        specService.add(spec);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 删除规格：
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        int delete = specService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功", delete);
    }

    /**
     * 根据id更新数据：
     * @param spec
     * @param id
     * @return
     */
    @PutMapping(value = "/update/{id}")
    public Result update(@RequestBody Spec spec, @PathVariable Integer id) {
        spec.setId(id);
        specService.update(spec);
        return new Result(true, StatusCode.OK, "更新成功");
    }

    /**
     * 根据id查询：
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<Spec> findById(@PathVariable Integer id) {
        Spec byId = specService.findById(id);
        return new Result<Spec>(true, StatusCode.OK, "查询成功", byId);
    }

    /**
     * 查询所有：
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Spec>> findAll() {
        List<Spec> specList = specService.findAll();
        return new Result<List<Spec>>(true, StatusCode.OK, "查询成功", specList);
    }

    /**
     * 多条件查询：
     * @param spec
     * @return
     */
    @PostMapping(value = "/searchobj")
    public Result<List<Spec>> findList(@RequestBody(required = false) Spec spec) {
        List<Spec> specList = specService.findList(spec);
        return new Result<List<Spec>>(true, StatusCode.OK, "查询成功", specList);
    }

    /**
     * 分页查询
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping(value = "/page/{pageSize}/{pageNum}")
    public Result<PageInfo> findPage(@PathVariable Integer pageSize, @PathVariable Integer pageNum) {
        PageInfo<Spec> pageInfo = specService.findPage(pageSize, pageNum);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 多条件分页查询：
     * @param spec
     * @param pageSize
     * @param pageNum
     * @return
     */
    @PostMapping(value = "/page/{pageSize}/{pageNum}")
    public Result<PageInfo> findPageList(@RequestBody(required = false) Spec spec, @PathVariable Integer pageSize, @PathVariable Integer pageNum) {
        PageInfo<Spec> pageList = specService.findPageList(spec, pageSize, pageNum);
        return new Result<>(true, StatusCode.OK, "查询成功", pageList);
    }
}
